#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use LWP::Simple;

if(not defined $ARGV[0])
{
	die "Need to provide config file as argument\n";
}
my ($ua, $agent, $device_id, $client_id, $geturl, $posturl, $port, $caddr, $command, $exitc, $response, $result, $output);
undef $command;
$ua = LWP::UserAgent->new;
my $cfile = $ARGV[0];
open FILE, "<$cfile" or die $!;
while (<FILE>)
{
	chomp $_;
	if($_ =~ /CLIENT-ID:(\d+)/)
	{
		$client_id = $1;
	}
	if($_ =~ /GET:(.*)/)
	{
		$geturl = $1;
	}
	if($_ =~ /DEVICE-ID:(\d+)/)
	{
		$device_id = $1;
	}
	elsif($_ =~ /POST:(.*)/)
	{
		$posturl = $1;
	}
}
$geturl = $geturl.'/'.$client_id;
#print "device-d - $device_id  client-id - $client_id geturl - $geturl posturl - $posturl\n";
#$caddr = "$url".":"."$port";
#Get the data from the geturl site
while(1)
{
	$response = $ua->get($geturl);
	print "Can't get $geturl -- ", $response->status_line
  	unless $response->is_success;

#print "THis is what we just GOT". $response->content;
	$response->content =~ m/"command":"(.*)"}/; 
	$command = $1;
#	print "THIS IS COMMAND AFTER GET $command \n";
	if($command !~ /\w+/)
	{
#print "In next loop\n";
		next;
	}
#print "$command\n";
	$exitc = system($command);
	if($exitc == 0)
	{
		$result = "DeviceID: $device_id;Message: SUCCESS;From: $client_id";
	}
	else
	{
		$result = "DeviceID: $device_id;Message: FAILED;From: $client_id";
	}
	undef $command;
#print "\n After execution Command changed to $command \n";
	#print "$result\n";
	$response = $ua->post($posturl,
 	[
		clientID => $device_id, 
   	response => $result, 
 	],
 	);
	print "Can't post to $posturl -- ", $response->status_line
  	unless $response->is_success;
	print $response->content;
	sleep 2;

}



=pod
if($string =~ m/Users? of (\w+):\s+(\(.+\))/){ 
# must test for successful pattern match
# or value may mistakenly be set to previous successful match!
    my $feature =$1;
    my $usage = $2;
    my $num_used=0;
    my $num_total=0;
    my $num_queued=0;

    if($usage =~ m/(\d+)\slicenses? (in use|Used)/ # handles variation
+s in Used Licenses
       ){ 
    $num_used=$1;
    }

    if($usage =~ m/(\d+)\slicenses? (issued|available)/ # handles vari
+ations in Total Licenses
       ){ 
    $num_total=$1;
    }

    if($usage =~ m/(\d+)\slicenses? (queued)/ # handles Queued license
+s
       ){ 
    $num_queued=$1;
    }

    # more IF statements to test for queued, reserved, etc.
    
    print "lmstat output: $string\n";
    print "feature name: $feature\n";
    print "usage string: $usage\n\n";
    print "Total $feature Licenses: $num_total\n";
    print "Total $feature Used Licenses: $num_used\n";
    print "Total $feature Queued Licenses: $num_queued\n";
    my $num_free=$num_total-$num_used-$num_queued;
    print "Approximate Free $feature Licenses: $num_free\n";

} else {
    # not a "Usage" line.  Perhaps parse out
    # the USERNAMES of each used license here...

=cut
